function [new_value, select_flag] = guivalue(slider_title, slider_min, slider_max, slider_value, slider_flag);

% GUIVALUE - select a value from a slider with the provided parameters
%
%    [NEWVAL, FLAG] = GUIVALUE(TITLE, MIN, MAX, OLDVAL, TYPE) creates a
%    figure window named TITLE containing a slider ranging from MIN to
%    MAX with initial value OLDVAL.  TYPE is a string ('int' or 'real')
%    that determines the numeric display and slider increments.  The
%    selected value is returned in NEWVAL, and FLAG indicates how the
%    slider figure was exited (0 if Cancel, 1 if OK).

% By:   S.C. Molitor (smolitor@bme.jhu.edu)
% Date: June 15, 1999

% check input parameters

new_value = 1;
select_flag = [];
if (nargin ~= 5)
   return
elseif (~ischar(slider_title))
   return
elseif (~isnumeric(slider_min))
   return
elseif (~isnumeric(slider_max))
   return
elseif (slider_max <= slider_min)
   return
elseif (~isnumeric(slider_value))
   return
elseif ((slider_value < slider_min) | (slider_value > slider_max))
   slider_value = slider_max;
   %return
elseif (~ischar(slider_flag))
   return
end

% initialize slider figure position for input

slider_width = 0.35;
slider_height = 0.10;
slider_pos = [(1 - slider_width)/2 (1 - slider_height)/2 slider_width slider_height];

% calculate an appropriate min & max step for slider
% values depend on whether slider is integer or real

if (strcmp(slider_flag, 'real'))
   log_step = 10^floor(log10(slider_max - slider_min));
   if ((slider_max - slider_min)/log_step < 5)
      log_step = log_step/10;
   end
   slider_step = [log_step/10 log_step]/(slider_max - slider_min);
   value_string = sprintf('%g', slider_value);
   callback_string = 'sprintf(''%g'', get(gcbo, ''Value''))';
else
   max_step = 10^floor(log10(slider_max - slider_min) - 1);
   if (max_step < 2)
      max_step = 2;
   end
   slider_step = [1 max_step]/(slider_max - slider_min);
   value_string = sprintf('%d', round(slider_value));
   callback_string = 'sprintf(''%d'', round(get(gcbo, ''Value'')))';
end

% create a new figure window to hold the slider

h_fig = figure('Color', [0.8 0.8 0.8], ...
   'Units', 'normalized', ...
   'Position', slider_pos, ...
   'MenuBar', 'none', ...
   'Name', slider_title, ...
   'NumberTitle', 'off', ...
   'CloseRequestFcn', 'set(gcf, ''UserData'', 0)');

% create text indicating slider value

h_text = uicontrol('Parent', h_fig, ...
   'Style', 'text', ...
   'Units', 'normalized', ...
   'Position', [0.05 0.75 0.90 0.15], ...
   'BackgroundColor', [0.8 0.8 0.8], ...
   'String', value_string);

% create a slider with the values provided

h_slider = uicontrol('Parent', h_fig, ...
   'Style', 'slider',...
   'Units', 'normalized', ...
   'Position', [0.05 0.50 0.90 0.20], ...
   'BackgroundColor', [0.8 0.8 0.8], ...
   'Callback', ['set(get(gcbo, ''UserData''), ''String'', ' callback_string ')'], ...
   'Max', slider_max, ...
   'Value', slider_value, ...
   'Min', slider_min, ...
   'SliderStep', slider_step, ...
   'UserData', h_text);

% create 'OK' & 'Cancel' buttons

h_ok = uicontrol('Parent', h_fig, ...
   'Style', 'pushbutton', ...
   'Units', 'normalized', ...
   'Position', [0.20 0.10 0.20 0.30], ...
   'Callback', 'set(gcf, ''UserData'', 1)', ...
   'String', 'OK');

h_cancel = uicontrol('Parent', h_fig, ...
   'Style', 'pushbutton', ...
   'Units', 'normalized', ...
   'Position', [0.60 0.10 0.20 0.30], ...
   'Callback', 'set(gcf, ''UserData'', 0)', ...
   'String', 'Cancel');

% wait for user to click button or close slider figure
% store user selection & delete slider figure

waitfor(h_fig, 'UserData');
if (strcmp(slider_flag, 'real'))
   new_value = get(h_slider, 'Value');
else
   new_value = round(get(h_slider, 'Value'));
end
select_flag = get(h_fig, 'UserData');
if (ishandle(h_fig))
   delete(h_fig);
end
return
